using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;

namespace Sec_scan漏洞扫描系统.Sec_Service
{
    /// <summary>
    /// 后台扫描服务
    /// </summary>
    public class Sec_PathScan_Service
    {
        /// <summary>
        /// 后台扫描方法
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string PathScan(string path)
        {
            Sec_Api.Sec_PathScan_Service_Api.Path = new StringBuilder();//初始化 Path
            string url = Sec_Api.Sec_PathScan_Service_Api.Url.ToString();//输入URL地址
            string paths = Sec_Api.Sec_AllAPI.Sqlclient(Sec_Api.Sec_LoopScan_Service_Api.P1);
            SQLiteConnection connection = new SQLiteConnection(paths);
            try
            {
                connection.Open();
                string sql = Sec_Api.Sec_AllAPI.PathInfos(Sec_Api.Sec_LoopScan_Service_Api.P1);//查询后台扫描
                SQLiteCommand command = new SQLiteCommand(sql, connection);
                using(SQLiteDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        string pathsinfo = reader["dic"].ToString();//后台地址
                        try
                        {
                            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + pathsinfo);
                            request.Method = "GET";
                            request.AllowAutoRedirect = false;
                            request.KeepAlive = false;
                            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                            if (response.StatusCode == HttpStatusCode.OK)
                            {
                                //能访问表示能打开，把存在的接口进行传出 ，不传出地址
                                Sec_Api.Sec_PathScan_Service_Api.UrlPathOk.Append(paths);
                            }
                        }
                        catch (Exception ex)
                        {
                            string Exp = ex.ToString();
                            Sec_Api.Sec_PathScan_Service_Api.ExpLoTions = Exp.ToString();
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                string exps = ex.ToString();
            }
            return path;
        }
        /// <summary>
        /// 代理的方式进行后台扫描
        /// </summary>
        /// <param name="proxx"></param>
        /// <returns></returns>
        public static string ProxxScan(string proxx)
        {
            Sec_Service.Sec_Proxx_Service.ProxySettring(Sec_Api.Sec_LoopScan_Service_Api.P1);
            return proxx;
        }
    }
}
